home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12410 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  49 lines

  1. Path: newsroom.hitc.com!kfreeman
  2. From: kfreeman@mandrake.HITC.COM (Keith Freeman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help: Containers for templated classes
  5. Date: 19 Mar 1996 18:12:07 GMT
  6. Organization: Hughes Team (EOSDIS)
  7. Message-ID: <KFREEMAN.96Mar19131207@mandrake.HITC.COM>
  8. References: <314740D4.23B1@mit.edu>
  9. NNTP-Posting-Host: mandrake.hitc.com
  10. In-reply-to: Imran Haq's message of Wed, 13 Mar 1996 17:40:36 -0400
  11. To: ihaq@mit.edu
  12.  
  13. In article <314740D4.23B1@mit.edu> Imran Haq <ihaq@mit.edu> writes:
  14.  
  15. > I'm kind of new to advanced usage of templates. Does anyone know how 
  16. > to put different template classes in a single container. Is this possible?
  17. >   myClass<int>   *type1;
  18. >   myClass<double>   *type;
  19. >   vector<myClass<????> >  container;
  20.  
  21. Simple: make all myClass instantiations inherit from a single base
  22. class.  It can be as simple as you like, e.g.:
  23.  
  24.     class myBase 
  25.     { 
  26.         virtual void StreamMe(ostream&os) = 0;
  27.     };
  28.     template <class T> class myClass : public myBase
  29.     {
  30.         ... 
  31.     };
  32.  
  33. then,
  34.  
  35.     vector<myClass<myBase> >  container;
  36.  
  37. It should be pretty easy to decide what common functionality (if any)
  38. you can put in myBase, based on what you want to do with the objects
  39. contained in container.  Any function you want to call using an object
  40. from the container must have a signature that's template-variable
  41. independent, and hence can be put in myBase (e.g. StreamMe).
  42.  
  43. keith freeman
  44. kfreeman@eos.hitc.com
  45.  
  46.  
  47.